﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmphuchoi : Form
    {
        public frmphuchoi()
        {
            InitializeComponent();
        }

        

        internal void btnthuchien_Click(object sender, EventArgs e)
        {
            
            try
            {
                Connection.sqlConnection.ChangeDatabase("master");
                if (txtfilebak.Text != null)
                {
                    if (txtnoichua.Text == null)
                    {
                        SqlCommand lenh = new SqlCommand("restore database QL_MUON_TRA_SACH_THU_VIEN from DISK=N'" + txtfilebak.Text + "'", Connection.sqlConnection);
                        int n = lenh.ExecuteNonQuery();
                        MessageBox.Show("Phục hồi thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Connection.sqlConnection.ChangeDatabase("QL_MUON_TRA_SACH_THU_VIEN");
                        this.Close();
                    }
                    else
                    {
                        if (txtnoichua.Text.Length == 3)
                        {
                            SqlCommand lenh = new SqlCommand("restore database QL_MUON_TRA_SACH_THU_VIEN from DISK=N'" + txtfilebak.Text + "' with move 'QUAN LY MUON TRA SACH THU VIEN' to '" + txtnoichua.Text + "QL_MUON_TRA_SACH_THU_VIEN.mdf', move 'QUAN LY MUON TRA SACH THU VIEN_log' to '" + txtnoichua.Text + "QL_MUON_TRA_SACH_THU_VIEN.ldf'", Connection.sqlConnection);
                            int n = lenh.ExecuteNonQuery();
                            MessageBox.Show("Phục hồi thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Connection.sqlConnection.ChangeDatabase("QL_MUON_TRA_SACH_THU_VIEN");
                            this.Close();
                        }
                        else
                        {
                            SqlCommand lenh = new SqlCommand("restore database QL_MUON_TRA_SACH_THU_VIEN from DISK=N'" + txtfilebak.Text + "' with move 'QUAN LY MUON TRA SACH THU VIEN' to '" + txtnoichua.Text + @"\QL_MUON_TRA_SACH_THU_VIEN.mdf', move 'QUAN LY MUON TRA SACH THU VIEN_log' to '" + txtnoichua.Text + @"\QL_MUON_TRA_SACH_THU_VIEN.ldf'", Connection.sqlConnection);
                            int n = lenh.ExecuteNonQuery();
                            MessageBox.Show("Phục hồi thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Connection.sqlConnection.ChangeDatabase("QL_MUON_TRA_SACH_THU_VIEN");
                            this.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
            
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmphuchoi_Load(object sender, EventArgs e)
        {
            
        }

        private void btnfilebak_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Mở File Sao Lưu";
            openFileDialog1.Filter = "Backup Files(*.bak)|*.bak|All Files(*.*)|*.*";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                txtfilebak.Text = openFileDialog1.FileName;
        }

        private void btnnoichua_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtnoichua.Text=folderBrowserDialog1.SelectedPath;
        }

        
    }
}
